package org.desktop.web.client.project.grid;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.desktop.web.client.project.proxy.RpcSubProjectProxy;
import org.desktop.web.client.project.service.ProjectService;
import org.desktop.web.client.project.service.ProjectServiceAsync;
import org.desktop.web.shared.domain.entities.SubProject;
import org.desktop.web.shared.domain.project.bean.SubProjectProperties;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfig;
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfigBean;
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoader;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;

public class SubProjectsGrid implements IsWidget {

	private ProjectServiceAsync projectService = GWT
			.create(ProjectService.class);

	private SubProjectProperties properties = GWT
			.create(SubProjectProperties.class);

	private Grid<SubProject> grid;
	private PagingLoader<FilterPagingLoadConfig, PagingLoadResult<SubProject>> loader ;
	RpcSubProjectProxy proxy ;

	@Override
	public Widget asWidget() {
		proxy = new RpcSubProjectProxy();
		 loader = new PagingLoader<FilterPagingLoadConfig, PagingLoadResult<SubProject>>(
				proxy) {

			@Override
			protected FilterPagingLoadConfig newLoadConfig() {
				// TODO Auto-generated method stub
				return new FilterPagingLoadConfigBean();
			}

		};
		ColumnConfig<SubProject, String> ccDescSubProject = new ColumnConfig<>(
				properties.descSubProject(), 200, "Nome progetto");
		ColumnConfig<SubProject, Date> ccStartDate = new ColumnConfig<>(
				properties.startDate(), 100, "Data inizio");
		ColumnConfig<SubProject, Date> ccEndDate = new ColumnConfig<>(
				properties.endDate(), 100, "Data fine");

		List<ColumnConfig<SubProject, ?>> l = new ArrayList<ColumnConfig<SubProject, ?>>();
		l.add(ccDescSubProject);
		l.add(ccStartDate);
		l.add(ccEndDate);

		// l.add(columnAddUsers);
		ColumnModel<SubProject> cm = new ColumnModel<SubProject>(l);

		final ListStore<SubProject> store = new ListStore<SubProject>(
				properties.key());
		loader.addLoadHandler(new LoadResultListStoreBinding<FilterPagingLoadConfig, SubProject, PagingLoadResult<SubProject>>(
				store));

		grid = new Grid<>(store, cm);
		grid.setLoader(loader);
		
		// TODO Auto-generated method stub
		return grid;
	}

	public void load(int id) {
		proxy.setId(Integer.toString(id));
		grid.setLoadMask(true);
		loader.load();
//		grid.setLoadMask(false);
		
	}

	public Grid<SubProject> getGrid() {
		return grid;
	}

	

}
